package principal;

import java.util.ArrayList;
import java.util.Vector;


import processing.core.PVector;

public class DeCasteljau {
	
	private static PVector deCasteljau(ArrayList<PVector> pontos, float t){
		int nPoints = pontos.size();
		int grau = nPoints - 1;
		
		Vector<PVector> tmp = new Vector<PVector>();
		
		// Copia todos os pontos para o vetor
		for (int k = 0; k < nPoints; k++) {
			PVector ponto = pontos.get(k);
			PVector p1 = new PVector (ponto.x, ponto.y);
			tmp.add(k, p1);
		}
		
		
		for (int i = 0; i < grau; i++) {
			nPoints = nPoints - 1;
			
			for (int j = 0; j < nPoints; j++) {
				float x = (float) ((tmp.get(j).x)*(1.0 - t) + (tmp.get(j + 1).x)*t);

				float y = (float) ((tmp.get(j).y)*(1.0 - t) + (tmp.get(j + 1).y)*t);
				
				
				tmp.get(j).x = x;
				tmp.get(j).y = y;
			}
			
		}
		
		return tmp.get(0);
	}
	
	public static PVector[] calcularCurvaBezier(ArrayList<PVector> pontos, int qtAvaliacoes)
	{
		if (pontos.size() <= 1)
			return /*new PVector[0]*/null;
		
		PVector[] avaliacoes = new PVector[qtAvaliacoes];
		
		float t = 0.0f;
		float inc = 1.0f/(qtAvaliacoes-1);
		for (int i = 0; i < qtAvaliacoes; i++)
		{
			avaliacoes[i] = deCasteljau(pontos, t);
			t += inc;
		}
		
		
		return avaliacoes;
	}
	
	/*public static void main(String[] args) {
		ArrayList<PVector> pontos = new ArrayList();
		
		pontos.add(0,new PVector(1, 2));
		pontos.add(1, new PVector(3, 4));
		pontos.add(2,new PVector(5, 6));
		PVector result = Bernstein.bernstein(pontos, 0.5f);
		System.out.println(result.x);
		System.out.println(result.y);
	}*/
}
